// select2二次封装
define([
	"select2js",
	"select2zh",
	"style!libJs/select2/select2.css"
], function (select2) {
	var select2Obj = {};

	// 设置参数
	select2Obj.defaultMohuValue = "0";

	// 模糊搜索
	// 参数一 自定义配置项
	// 参数二 插件配置项
	select2Obj.mohuSearch = function(opt, select2Config){
		// 设置配置项
		var defaults = {
			ele: "mohu_search_1",//select选择框 id
			data: "",//数据源格式必须是 [{key: val},{key: val}]
			dataMap: "",//字段映射（data id 和 text 映射）{id: "id", text: "text"}
			changeFun: "",//change事件回调
			defaultOptionVal: "请选择",//默认项的文本及id值
			selectedId: "",//默认选中的id
		}
		var config = $.extend({}, defaults, opt);
		// 处理数据
		var dataSelect = [{id:this.defaultMohuValue, text:config.defaultOptionVal}];
		for(var i = 0; i < config.data.length; i++){
			var obj = {};
			for(var j in config.dataMap){
				obj[j] = config.data[i][config.dataMap[j]];
				if(config.data[i][config.dataMap["id"]] == config.selectedId){
					obj.selected = true;
				}
			}
			dataSelect.push(obj);
		}
		// 生成结构
		$("#" + config.ele)
			.attr("data-search-type", "mohu")
			.attr("data-reset-val", config.selectedId ? config.selectedId : this.defaultMohuValue);
		if( !$("#" + config.ele + "MohuSearchFolder").length ){
			$("#" + config.ele).wrap( $("<div id="+ config.ele + "MohuSearchFolder" +" class='ele_form mohu_search'></div>") );
		}
		// 设置select2配置
		var defaultSelect = {
			width: "100%",
			language: 'zh-CN',
			dropdownParent: $("#" + config.ele + "MohuSearchFolder"),
		 	data: dataSelect
		}
		var oSelect2Configs = $.extend({}, defaultSelect, select2Config);
		// 调用方法
		var oSelect2 = $("#" + config.ele).select2(oSelect2Configs).change(function(){
			config.changeFun && config.changeFun( $("#" + config.ele) );
		});
	}

	// function select2(){
	// 	$.fn.select2.defaults.set("language", "zh-CN");
	// }

	return select2Obj;
})